﻿using System;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Modules.Cms;
using MQTTServer.Services;

namespace MQTTServer
{
    class Program
    {
        [STAThread]
        static void Main()
        {

            var builder = new HostBuilder().ConfigureServices((hostContext, services) =>
           {
               ConfigureServices(services);
           }).ConfigureAppConfiguration(options =>
           {
               options.AddJsonFile(Path.Combine(Environment.CurrentDirectory, "appsettings.json"), optional: true, reloadOnChange: true);
           });

            //// 创建配置提供程序
            //var builder = new ConfigurationBuilder()
            //    .SetBasePath(Directory.GetCurrentDirectory())
            //    .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true); // 加载配置文件

            //IServiceProvider serviceProvider = new ServiceCollection().BuildServiceProvider();
            //var cmsOrderService = serviceProvider.GetService<CmsOrderService>();

            // 创建Service实例并注入配置数据（以MyService类为例）
            //var service = new MQTTService();
            //service.StartMqttServer().Wait();
            //Console.ReadLine();

            //MQTT客户端
            var client = new MQTTClient();
            client.StartClient();
            Console.ReadLine();
        }

        /// <summary>
        /// 注入服务
        /// </summary>
        /// <param name="services"></param>
        public static void ConfigureServices(IServiceCollection services)
        {
            services.AddScoped<IConfiguration>();
            services.AddScoped<CmsOrderService>();
            //日志
            services.AddLogging(configure =>
            {
                string path = Path.Combine("Configs", "log4net.config");
                configure.AddLog4Net(path);
            });

        }
    }
}